﻿using GetData.webProductClass;
using jutehui.GetData;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    public class HuiHuiWang
    {
        HtmlAgilityPack.HtmlDocument doc;
        public HuiHuiWang()
        {
            doc = new HtmlAgilityPack.HtmlDocument();
        }
        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url)
        {
            //  string url = "http://www.shihuo.cn/";
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            var pList = GetProductByRegex(htmlText);
            if (pList == null)
            {
                return "";
            }
            string msg = "";
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return "";
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();

            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count.ToString(); ;
            }
            else
            {
                msg = "惠惠网：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return "";
            }
        }
        public List<Product> GetProductByRegex(string htmlText)
        {
            List<Product> plist = new List<Product>();
            doc.LoadHtml(htmlText);
            var collec = doc.DocumentNode.SelectNodes("//div[@class='list-item1']");
            var list = new List<string>();
            if (collec == null || collec.Count < 1)
            {
                return null;
            }
            for (int index = 0; index < collec.Count; index++)
            {
                list.Add(collec[index].InnerHtml);
            }

            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                    string html = list[index];
                    product = GetProduct(html);
                    if(product==null)
                    {
                        continue;
                    }
                    product.UpdateTime = DateTime.Now;
                    product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                }
                catch (Exception ex)
                {
                    string msg = "惠惠网：GetProductByRegex " + ex.Message.ToString() + ":" + index;
                    CustomMethod.WriteLog(msg);
                    continue;
                }
                plist.Add(product);
            }
            var newList = CustomMethod.GetDistinctProdcuts(plist);
            newList.Reverse();
            return newList;
        }

        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "慧慧网";
            pro.TypeId = 1;
            doc.LoadHtml(htmlContent);
            //名称
            var namelist = doc.DocumentNode.SelectNodes("//div[@class='ctn']/h3");
            if (namelist != null)
            {
                pro.Pname = namelist[0].InnerText;
            }

            //图片
            var picL = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='pic']/img", "data-original");
            if (picL != null)
            {
                pro.ImgUrl = picL[0];
            }
            //价格
            var priL = doc.DocumentNode.SelectNodes("//div[@class='ctn']/h4");
            if (priL != null && priL.Count > 0)
            {
                pro.Price = priL[0].InnerText;
            }
            //数据源
            var sourL = CustomMethod.GetValue(doc, "//div[@class='fuc clearfix']/span");
            if (!string.IsNullOrEmpty(sourL))
            {
                if (sourL.Contains("惠惠"))
                {
                    return null;
                }
                pro.Source = sourL;
            }


            var detailurl = CustomMethod.GetAttr(doc, htmlContent, "//a", "href");
            string conthtml = "";//详细页面
            //详细
            if (detailurl != null && detailurl.Count > 0)
            {
                string durl = "http://www.huihui.cn" + detailurl[0];
                conthtml = CustomMethod.GetHtmlByHttpHelper(durl);
                doc.LoadHtml(conthtml);
                var desList = doc.DocumentNode.SelectNodes("//div[@class='editor-mod']/p");
                string desStr = "";
                if (desList != null)
                {
                    for (int k = 0; k < desList.Count; k++)
                    {
                        if (desList[k].InnerText == "")
                        {
                            continue;
                        }
                        else if (desList[k].InnerText.Contains("小编良心"))
                        {
                            continue;
                        }
                        desStr += "<p>" + desList[k].InnerText + "</p>";
                    }
                    pro.Description = desStr;
                }
                else
                {
                    pro.Description = pro.Pname;
                }
                //链接 
                var urL = CustomMethod.GetAttr(doc, conthtml, "//a[@class='btn-buy']", "href");
                if (urL != null && urL.Count>0)
                {
                    string dirUrl = "http://www.huihui.cn" + urL[0];
                    if (urL[0].Contains("http"))
                    {
                        dirUrl = urL[0];
                    }
                    string productUrl = "";
                    string trueUrl = CustomMethod.GetTrueUrl(dirUrl, ref productUrl);
                    pro.Memo = productUrl;
                    pro.Plink = trueUrl;
                    if (pro.Plink == "")
                    {
                        if (productUrl == "")
                        {
                            CustomMethod.WriteLog("惠惠网：没有抓取到商品链接");
                        }
                        return null;
                    }
                }
                else
                {
                    return null;
                }

                //分类
                var ty = CustomMethod.GetValue(doc, "//div[@class='list-info']/span[@class='tag']");
                if (!string.IsNullOrEmpty(ty))
                {

                    var tagList = ty.Trim().Split(' ').ToList();
                    var tag = tagList[tagList.Count - 1];
                    var tyid = ProductTypeEnum.GetProductType(tag, typeof(ProductTypeEnum.huihuiType));
                    pro.ProductTypeId = tyid;
                }
                else
                {
                    pro.ProductTypeId = 16;
                }
                ////详细图
                //var despics = CustomMethod.GetAttr(doc, conthtml, "//div[@class='con clearfix editor-frame frame']/p/a/img", "src");
                //if (despics != null)
                //{
                //    if (despics.Count == 1)
                //    {
                //        pro.ImgUrl2 = despics[0];
                //    }
                //    if (despics.Count == 2)
                //    {
                //        pro.ImgUrl2 = despics[0];
                //        pro.ImgUrl3 = despics[1];
                //    }
                //    else if (despics.Count >= 3)
                //    {
                //        pro.ImgUrl2 = despics[0];
                //        pro.ImgUrl3 = despics[1];
                //        pro.ImgUrl4 = despics[2];
                //    }
                //}
            }
            else
            {
                return null;
            }
            return pro;
        }
    }
}
